package com.recharge.api.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.recharge.api.dto.request.CallBackRequest;
import com.recharge.api.dto.request.TransactionRequest;
import com.recharge.api.dto.response.*;
import com.recharge.api.service.ApiService;
import com.recharge.api.utils.PartyUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
@Slf4j
@Service
public class ApiServiceImpl implements ApiService {

    @Override
    public TransactionResponse processTransaction(TransactionRequest request) {
        try {
            TransactionResponse response = new TransactionResponse();
            String apiUrl = String.format(
                    "https://api.RechargeExchange.com/Transaction?userid=%s&token=%s&opcode=%s&number=%s&amount=%f&transid=%s",
                    request.getUserid(), request.getToken(), request.getOpcode(), request.getNumber(), request.getAmount(), request.getTransid());
            String transactionResp = PartyUtil.sendRequest(apiUrl, "POST");
            JSONObject jsonObject = JSON.parseObject(transactionResp);
            response.setStatus(jsonObject.get("status").toString());
            response.setMessage(jsonObject.get("message").toString());
            response.setTransid(jsonObject.get("transid").toString());
            response.setOptransid(jsonObject.get("optransid").toString());
            response.setNumber(jsonObject.get("number").toString());
            response.setAmount((Double) jsonObject.get("amount"));
            response.setBalance((Double) jsonObject.get("balance"));
            return response;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public BalanceResponse getBalance(String userId, String token) {
        try {
            String apiUrl = String.format(
                    "https://status.rechargeexchange.com/API.asmx/BalanceNew?userid=%s&token=%s",
                    userId, token
            );
            String balancesResp = PartyUtil.sendRequest(apiUrl, "POST");
            JSONObject jsonObject = JSON.parseObject(balancesResp);
            BalanceResponse response = new BalanceResponse();
            response.setStatus(jsonObject.get("status").toString());
            response.setMessage(jsonObject.get("message").toString());
            response.setBalance((Double) jsonObject.get("balance"));
            return response;
        }catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public StatusResponse getTransactionStatus(String userId, String token, String transId) {
        try {
            String apiUrl = String.format(
                    "https://status.rechargeexchange.com/API.asmx/TransactionStatus?userid=%s&token=%s&transid=%s",
                    userId, token, transId);
            String transactionStatusResp = PartyUtil.sendRequest(apiUrl, "POST");
            JSONObject jsonObject = JSON.parseObject(transactionStatusResp);
            StatusResponse response = new StatusResponse();
            response.setStatus(jsonObject.get("status").toString());
            response.setMessage(jsonObject.get("message").toString());
            response.setAmount((Double) jsonObject.get("amount"));
            response.setNumber(jsonObject.get("number").toString());
            response.setOptransid(jsonObject.get("transid").toString());
            response.setOptransid(jsonObject.get("optransid").toString());
            return response;
        }catch (Exception e){
            log.error("error message =" , e.getMessage());
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public JSONArray getAllOperators() {
        try {
            String apiUrl = String.format(
                    "https://api.RechargeExchange.com/API.asmx/OperatorList");
            String OperatorsResp = PartyUtil.sendRequest(apiUrl, "POST");
            JSONArray jsonArray = JSON.parseArray(OperatorsResp);

            return jsonArray;
        } catch (Exception e) {
            log.error("error message =" , e.getMessage());
            e.printStackTrace();
        }
        return null;

    }

    @Override
    public String callBackResponse(CallBackRequest callBackRequest) {
        try{
            String apiUrl = String.format(
                    "https://www.yourdomain.com/callback?status=%s&opid=%s&yourtransid=%s&txnid=%s&number=%s&amount=%s&message=%s",
                    callBackRequest.getStatus(), callBackRequest.getOpid(), callBackRequest.getYourtransid(), callBackRequest.getTxnid(), callBackRequest.getNumber(), callBackRequest.getAmount(), callBackRequest.getMessage());
            String sendGetRequest = PartyUtil.sendRequest(apiUrl, "GET");
            return sendGetRequest;
        }catch (Exception e){
            log.error("error message =" , e.getMessage());
            e.printStackTrace();
        }
        return "";
    }

    private OperatorResponse createOperator(String name, String id) {
        OperatorResponse operator = new OperatorResponse();
        operator.setOperatorName(name);
        operator.setOperatorId(id);
        return operator;
    }
}